{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": 3
  },
  "orig_nbformat": 2
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from rouge import rouge\n",
    "import json\n",
    "from tqdm.notebook import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rouge = Rouge()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_ref_data = []\n",
    "all_pred_data = []\n",
    "with open(\"/data/paraphrase/data_v2/quora/dev.json\", \"r\", encoding=\"utf-8\") as f:\n",
    "    for line in tqdm(f.readlines()):\n",
    "        line = json.loads(line.strip())\n",
    "        all_ref_data.append(line[\"tgt\"])\n",
    "\n",
    "with open(\"/data/paraphrase/results/quora_ft.txt\", \"r\", encoding=\"utf-8\") as f:\n",
    "    for line in tqdm(f.readlines()):\n",
    "        line = line.strip()\n",
    "        all_pred_data.append(line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(all_pred_data[0])\n",
    "print(all_ref_data[0])\n",
    "rouge.get_scores(all_pred_data[0], all_ref_data[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_r1_score = []\n",
    "all_r2_score = []\n",
    "all_rL_score = []\n",
    "for pred, ref in zip(all_pred_data, all_ref_data):\n",
    "    score = rouge.get_scores(pred, ref)[0]\n",
    "    all_r1_score.append(score['rouge-1']['f'])\n",
    "    all_r2_score.append(score['rouge-2']['f'])\n",
    "    all_rL_score.append(score['rouge-l']['f'])\n",
    "\n",
    "print(sum(all_r1_score)/len(all_r1_score))\n",
    "print(sum(all_r2_score)/len(all_r2_score))\n",
    "print(sum(all_rL_score)/len(all_rL_score))"
   ]
  }
 ]
}